//
//  CZTabBarController.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/8.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

class CZTabBarController: UITabBarController {
    
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
       
        
        

        self.addChildViewController()
    
        
        //kvc 给tabBar 赋值 相当于重写了tababr的方法 这样才能显示出新的tabbar
        
        let composeBtn = CZcomeposeButton()
        
//        self.tabBar = composeBtn//系统默认的tabbar是getonly的不能够修改或者赋值  只有通过kvc方法才能够实现重写tabbar
        
        
       setValue(composeBtn, forKey: "tabBar")
        
        // MARK: - ******************* 实现闭包 ********************
        //实现闭包
        composeBtn.composeclouse = {
         print("点击中间加号按钮")
        }
        
        
        
    }
    
    // MARK: - ******************* 添加tabbar ********************
    func addChildViewController() {
        
        
        setVC(CZHomeTableViewController (), title:  "首页", imageName: "tabbar_home", selectedImage: "tabbar_home_highlighted")
        setVC(CZDiscoverTableViewController (), title:   "发现", imageName: "tabbar_discover", selectedImage: "tabbar_discover_highlighted")
        
        setVC(CZmessageTableViewController (), title:  "消息", imageName:  "tabbar_message_center", selectedImage: "tabbar_message_center_highlighted")
        
        setVC(CZProfileTableViewController (), title:  "我", imageName:  "tabbar_profile", selectedImage: "tabbar_profile_highlighted")

    }

    func test () {
    
        
        let HomeVC = CZHomeTableViewController()
        
        
        HomeVC.title = "首页"
        
        HomeVC.tabBarItem.image = UIImage(named: "tabbar_home")
        HomeVC.tabBarItem.selectedImage = UIImage(named: "tabbar_home_highlighted")
        
        let navVC1 = UINavigationController(rootViewController: HomeVC)
        let DiscoverVC = CZDiscoverTableViewController()
        
        DiscoverVC.title = "发现"
        
        DiscoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
        DiscoverVC.tabBarItem.selectedImage = UIImage(named: "tabbar_discover_highlighted")
        
        let navVC2 = UINavigationController(rootViewController: DiscoverVC)
        
        let MessageVC = CZmessageTableViewController()
        
        MessageVC.title = "消息"
        
        MessageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
        MessageVC.tabBarItem.selectedImage = UIImage(named: "tabbar_message_center_highlighted")
        
        let navVC3 = UINavigationController(rootViewController: MessageVC)
        
        let ProfileVC = CZProfileTableViewController()
        
        ProfileVC.title = "我"
        
        ProfileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
        ProfileVC.tabBarItem.selectedImage = UIImage(named: "tabbar_profile_highlighted")
        
        let navVC4 = UINavigationController(rootViewController: ProfileVC)
        
        
        
        addChildViewController(navVC1)
        addChildViewController(navVC2)
        addChildViewController(navVC3)
        addChildViewController(navVC4)
        
        
        
        

    }
    
    
//封装的方法
    func setVC (controller: UIViewController, title: String, imageName: String , selectedImage: String) {

        
        controller.title = title
        
        controller.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//原始化渲染可以避免系统渲染成蓝色

        controller.tabBarItem.selectedImage = UIImage(named: selectedImage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//原始化渲染可以避免系统渲染成蓝色

        let nav = UINavigationController(rootViewController: controller)
    
    
        
        // 设置文字的颜色
           controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)], forState: UIControlState.Selected)
        
        
        addChildViewController(nav)
        
        
        
        
        
    
    }
    
    

    
    
    
 }
